﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace QuanLyKhachSan
{
    public partial class frmChuyenPhong : DevExpress.XtraEditors.XtraForm
    {
        DataClassesKhachSanDataContext db = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString);
        public frmChuyenPhong()
        {
            InitializeComponent();
        }
        public int cttp_id;
        public frmSoDoPhong frmsodo;
        static Decimal tongtiendichvu, tongtienthucan, tongtienphong;
        static int pid, tpid;
        private void frmChuyenPhong_Load(object sender, EventArgs e)
        {
           
            //hien thi tien phong hien tai
            this.fillTienPhongHienTai();



            this.fillCbPhong();

           

        }
        void fillCbPhong()
        {
            //var phong = db.Kiem_tra_Dat_phong(DateTime.Parse(deNgayDen.EditValue.ToString()), DateTime.Parse(deNgayDi.EditValue.ToString()));
            var phong = from p in db.PHONGs
                        join dg in db.DON_GIA_PHONGs
                        on new { p.LP_ID, p.CP_ID }
                        equals new { dg.LP_ID , dg.CP_ID }
                        where p.P_ID != pid
                        select new
                        {
                            p.P_MA,
                            p.P_ID,
                            p.KHU_VUC.KV_TEN,
                            p.CO_PHONG.CP_CO,
                            p.LOAI_PHONG.LP_LOAI,
                            dg.DG_GIA_GIO, dg.DG_GIA_NGAY
                        };
            lePhong.Properties.DataSource = phong;
            lePhong.Properties.DisplayMember = "P_MA";
            lePhong.Properties.ValueMember = "P_ID";
            lePhong.ItemIndex = 0;

            
        }
        void fillDichVu()
        {
            var dichvu = from dv in db.SU_DUNGs
                         where dv.SD_CHITIETTPID == cttp_id
                         select new
                         {
                             dv.CHI_TIET_THUE_PHONG.TP_ID,
                             dv.SD_CHITIETTPID,
                             dv.SD_DICHVUID,
                             dv.SD_THOIDIEM,
                             dv.SD_DONGIA,
                             dv.SD_SL,
                             dv.DICH_VU.DV_DONVITINH,
                             dv.DICH_VU.DV_TEN,
                             THANHTIEN = dv.SD_SL * dv.SD_DONGIA,

                         };
            //gcDichVu.DataSource = dichvu;

           
            if (dichvu.Count() > 0)
            {
                var sumDichVu = dichvu.Select(s => s.THANHTIEN).Sum();
                tongtiendichvu = sumDichVu;
            }
        }
        void fillMonAn()
        {
            var monan = from m in db.GOI_THUC_AN_NHANHs
                        where m.CTTP_ID == cttp_id
                        select new
                        {
                            m.GTAN_ID,
                            m.CTTP_ID,
                            m.DT_ID,
                            m.G_THOIDIEM,
                            m.G_SL,
                            m.G_DONGIA,
                            m.THUC_UONG_THUC_AN_NHANH.DOI_TUONG.DT_DVI,
                            m.THUC_UONG_THUC_AN_NHANH.DOI_TUONG.DT_TEN,
                            THANHTIEN = m.G_SL * m.G_DONGIA
                        };
           // gcThucAn.DataSource = monan;

            if (monan.Count() > 0)
            {
                var sumThucAn = monan.Select(s => s.THANHTIEN).Sum();

                tongtienthucan = sumThucAn;
            }

        }
        void fillTienPhongHienTai()
        {
            this.fillDichVu();
            this.fillMonAn();


            var thuephong = (from t in db.CHI_TIET_THUE_PHONGs
                             where t.CTTP_ID == cttp_id
                             select t).Single();
            //deNgayDen.EditValue = thuephong.CTTP_NGAYBD;
            //deNgayDi.EditValue = DateTime.Now;
            //txtDonGia.Text = thuephong.CTTP_DONGIA.ToString();

            pid = int.Parse(thuephong.P_ID.ToString());
            tpid = int.Parse(thuephong.TP_ID.ToString());
            TimeSpan ngay = DateTime.Now - thuephong.CTTP_NGAYBD;

            if (thuephong.THUE_PHONG.TP_LOAI_GIA == true)
            {
                if (ngay.TotalDays < 1)
                {

                    tongtienphong = Decimal.Parse(thuephong.CTTP_DONGIA.ToString());
                }
                else
                {
                    tongtienphong = int.Parse(ngay.TotalDays.ToString()) * Decimal.Parse(thuephong.CTTP_DONGIA.ToString());
                }
            }
            else
            {
                tongtienphong = int.Parse(ngay.TotalHours.ToString()) * Decimal.Parse(thuephong.CTTP_DONGIA.ToString());
            }
            //txtTienPhong.Text = tongtienphong.ToString();
            //Decimal phuthu = Decimal.Parse(txtDonGia.Text) * int.Parse(txtPhuThu.EditValue.ToString());
            txtTienPhongHienTai.Text = (tongtiendichvu + tongtienthucan + tongtienphong).ToString();
        }

        private void btChuyen_Click(object sender, EventArgs e)
        {
            this.ChuyenPhong();
        }

        void ChuyenPhong()
        {
            try
            {

          
            //kiem tra trang thai phong
            var trangthai = db.PHONGs.Single(t => t.P_ID == pid);

            var datphong = from d in db.CHI_TIET_DAT_PHONGs
                           where d.P_ID == trangthai.P_ID
                           && d.DAT_PHONG.DP_NGAYBD >= DateTime.Today
                           select d;

            if (datphong.Count() > 0)
            {
                trangthai.TT_ID = 6;
            }
            else
            {
                trangthai.TT_ID = 7;
            }
            var chitiet2 = db.CHI_TIET_THUE_PHONGs.Single(c => c.CTTP_ID == cttp_id);

            chitiet2.CTTP_NGAYKT = DateTime.Today;

            chitiet2.CTTP_CHECKOUT = true;

            //var chitiet = from c in db.CHI_TIET_THUE_PHONGs where c.TP_ID == tpid select c;

           //chinh sua so tien cua phong truoc
            var thuephong = db.THUE_PHONGs.Single(p => p.TP_ID == tpid);

            thuephong.TP_TONGCONG = thuephong.TP_TONGCONG + Decimal.Parse(txtTienPhongHienTai.Text);


                //kiem tra tat ca khach hang thue phong hien tai
            var KHtrutai = (from t in db.TRU_TAIs select t).ToList ();
                //them chi tiet thue phong moi

            CHI_TIET_THUE_PHONG chitiet = new CHI_TIET_THUE_PHONG();
            chitiet.THUE_PHONG = thuephong;
            chitiet.P_ID = int.Parse(lePhong.EditValue.ToString());

            //cap nhat trang thai cua phong
            var phong = db.PHONGs.Single(p => p.P_ID == int.Parse(lePhong.EditValue.ToString()));
            phong.TT_ID = 5;

            chitiet.CTTP_NGAYBD = DateTime.Now;
            chitiet.CTTP_NGAYKT = thuephong .TP_NGAYKT;

            if (thuephong.TP_LOAI_GIA == true)
            {
                chitiet.CTTP_DONGIA = Decimal.Parse(lePhong.GetColumnValue("DG_GIA_NGAY").ToString());
            }
            else
            {
                chitiet.CTTP_DONGIA = Decimal.Parse(lePhong.GetColumnValue("DG_GIA_GIO").ToString());
            }
            db.CHI_TIET_THUE_PHONGs.InsertOnSubmit(chitiet);

            for (int i = 0; i < KHtrutai.Count; i++)
            {
                        TRU_TAI trutai = new TRU_TAI();
                        trutai.KH_ID = KHtrutai[i].KH_ID;
                        trutai.CHI_TIET_THUE_PHONG = chitiet;
                        chitiet.TRU_TAIs.Add(trutai);
            }



            db.SubmitChanges();

            XtraMessageBox.Show("Chuyển đến phòng " +  lePhong .GetColumnValue ("P_MA").ToString ().ToUpper()  + " thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            this.Close();

            }
            catch (Exception)
            {

                throw;
            }
        }

        private void frmChuyenPhong_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmsodo.FillData();
        }

       
    }
}